package com.hxjb.controller;

import com.huamo.base.dao.BaseDao;
import com.huamo.common.exception.ProcessException;
import com.huamo.model.result.CommonJsonResult;
import com.hxjb.entity.User;
import com.hxjb.mapper.UserMapper;
import com.hxjb.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * Created by wt on 2018/4/19.
 */
@RestController
public class UserController {
    @Autowired
    private BaseDao baseDao;
    @Autowired
    private IUserService userService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 通过baseDao
     * @param id
     * @return
     */
    @GetMapping(value = "getById")
    public CommonJsonResult getById (@RequestParam(required = true)Integer id) {

        CommonJsonResult jsonResult = new CommonJsonResult();
        jsonResult.setInfoMap("user",baseDao.selectOne("UserMapper.getById",id));

        return jsonResult;
    }

     /**
     * 通过userMapper
     * @param id
     * @return
     */
    @GetMapping(value = "getById2")
    public CommonJsonResult getById2 (@RequestParam(required = true)Integer id) {

        CommonJsonResult jsonResult = new CommonJsonResult();
        jsonResult.setInfoMap("user",userMapper.getById2(id));

        return jsonResult;
    }



    /**
     * 通过注解实现事务
     * @param id
     * @param cityid
     * @return
     */
    @PutMapping(value = "updateCityidById")
    @Transactional//事务，在启动类上配置@EnableTransactionManagement
    public CommonJsonResult updateCityidById (@RequestParam(required = true)Integer id,
                                              @RequestParam(required = true)Integer cityid) {

        CommonJsonResult jsonResult = new CommonJsonResult();

        User user4Update = new User();
        user4Update.setId(id);
        user4Update.setCityid(cityid);
        baseDao.update("UserMapper.updateCityidById", user4Update);

        int i = 1 / 0;

        return jsonResult;
    }

    @PutMapping(value = "testTransactionalByMethodInMethod")
    public CommonJsonResult testTransactionalByMethodInMethod (@RequestParam(required = true)Integer id,
                                              @RequestParam(required = true)Integer cityid) {

        CommonJsonResult jsonResult = new CommonJsonResult();

        User user = new User();
        user.setId(id);
        user.setCityid(cityid);
        userService.testTransactionalByMethodInMethod(user);

        return jsonResult;
    }



    /**
     * 通过service的方法名实现事务
     * @param id
     * @param cityid
     * @return
     */
    @PutMapping(value = "updateCityidById2")
    public CommonJsonResult updateCityidById2 (@RequestParam(required = true)Integer id,
                                              @RequestParam(required = true)Integer cityid) {

        CommonJsonResult jsonResult = new CommonJsonResult();

        User user4Update = new User();
        user4Update.setId(id);
        user4Update.setCityid(cityid);
        userService.updateCityidById2(user4Update);

        return jsonResult;
    }


    @PostMapping(value = "save")
    public CommonJsonResult save (@RequestParam(required = true)String mobile) {

        CommonJsonResult jsonResult = new CommonJsonResult();
        User user = new User();
        user.setMobile(mobile);

        userMapper.save(user);

        jsonResult.setInfoMap("id",user.getId());

        return jsonResult;
    }



}
